package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	@Autowired
	private StandardService standardService;
	
	private Standard standard = new Standard();
	
	
	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return standard;
	}
	@Action(value="standard_add",results={@Result(type="redirect",name="success",location="./pages/base/standard.html")})
	public String standardAdd(){
		standardService.add(standard);
		System.out.println("添加成功");
		return SUCCESS;
	}
	
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "StandardPageAction", results = { @Result(name = "success", type = "json") })
	public String StandardPageAction() {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Standard> pp = standardService.findPageData(pageable);
		Map<String,Object> map = new HashMap<>();
		//String total = null;
		map.put("total", pp.getTotalElements());
		map.put("rows", pp.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	@Action(value="Standard_findAll",results={@Result(name="success",type="json")})
	public String Standard_findAll(){
		List<Standard> list = standardService.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
